package com.medical.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.medical.core.result.PageResult;
import com.medical.core.result.Result;
import com.medical.system.model.form.RoleForm;
import com.medical.system.model.query.RolePageQuery;
import com.medical.system.model.vo.RolePageVO;
import com.medical.system.service.SysRoleService;
import com.medical.web.model.Option;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "02.角色接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/role")
public class SysRoleController {

    private final SysRoleService roleService;

    @Operation(summary = "角色分页列表")
    @GetMapping("/page")
    public PageResult<RolePageVO> getRolePage(@ParameterObject RolePageQuery pageQuery) {
        return PageResult.success(roleService.getRolePage(pageQuery));
    }

    @Operation(summary = "角色下拉列表")
    @GetMapping("/options")
    public Result<List<Option>> roleOptions()  {
        return Result.success(roleService.listRoleOptions());
    }

    @Operation(summary = "添加角色")
    @PostMapping
    @SaCheckPermission("sys:role:add")
    public Result addRole(@Valid @RequestBody RoleForm roleForm) {
        return Result.judge(roleService.saveRole(roleForm));
    }

    @Operation(summary = "获取角色表单数据")
    @GetMapping("/{roleId}/form")
    public Result<RoleForm> getRoleForm(@Parameter(description = "角色ID")  @PathVariable  Long roleId) {
        return Result.success(roleService.getRoleForm(roleId));
    }

    @Operation(summary = "修改角色")
    @PutMapping("/{id}")
    @SaCheckPermission("sys:role:update")
    public Result updateRole(@Valid @RequestBody RoleForm roleForm) {
        return Result.judge(roleService.saveRole(roleForm));
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{ids}")
    @SaCheckPermission("sys:role:delete")
    public Result deleteRoles(@Parameter(description = "角色ID，多个以英文逗号分割") @PathVariable(required = true) String ids) {
        return Result.judge(roleService.deleteRoles(ids));
    }

    @Operation(summary = "修改角色状态")
    @PatchMapping("/{roleId}/status")
    @SaCheckPermission("sys:role:update_status")
    public Result updateRoleStatus(@Parameter(description = "角色ID") @PathVariable(required = true) Long roleId,
                                   @Parameter(description = "状态 1：启用、0：禁用") @RequestParam(required = true) Integer status) {
        return Result.judge(roleService.updateRoleStatus(roleId, status));
    }

    @Operation(summary = "获取角色的菜单ID集合")
    @GetMapping("/{roleId}/menuIds")
    public Result getRoleMenuIds(@Parameter(description = "角色ID") @PathVariable(required = true) Long roleId) {
        return Result.success(roleService.getRoleMenuIds(roleId));
    }

    @Operation(summary = "分配菜单权限给角色")
    @PutMapping("/{roleId}/menuIds")
    @SaCheckPermission("sys:role:assign_menu")
    public Result assignMenuToRole(@Parameter(description = "角色ID") @PathVariable(required = true) Long roleId,
                                   @Parameter(description = "菜单ID集合") @RequestParam(required = true) List<Long> menuIds) {
        return Result.judge(roleService.assignMenusToRole(roleId, menuIds));
    }
}
